package io.github.tangwan.designpattern.creational.singleton;

/**
 * 懒加载
 * 登记式/静态内部类
 * 利用了 classloader 机制来保证初始化 instance 时只有一个线程
 * @author tangwan
 * @version V1.0
 * created on 2021/9/27 11:27
 * @since JDK 1.8
 */
public class Singleton5 {

    private static class SingletonHolder {
        private static final Singleton5 INSTANCE = new Singleton5();
    }

    private Singleton5() {
    }

    /**
     * SingletonHolder使用时才会被类加载器加载,类加载器为了避免重复加载类是线程安全的
     * @return
     */
    public static final Singleton5 getInstance() {
        return SingletonHolder.INSTANCE;
    }

}
